﻿namespace QLPhongKham.Access.BenhNhan
{
    using ConfigDatabase.Class;
    using QLPhongKham.Access;
    using QLPhongKham.BaseClass.BenhNhan;
    using QLPhongKham.BaseClass.Chung;
    using QLPhongKham.BaseClass.VienPhi;
    using System;
    using System.Data;

    public class BNDanhMuc
    {
        private ConnectDatabase _condata;
        private General _xuli;

        public BNDanhMuc()
        {
            this._condata = new ConnectDatabase();
            this._xuli = new General(this._condata);
        }

        public BNDanhMuc(ConnectDatabase condata)
        {
            this._condata = condata;
            this._xuli = new General(this._condata);
        }

        public string f_get_tentinhphuongxa(string maviettat)
        {
            try
            {
                return this.f_loaddata_tinhphuongxa(maviettat, 0L).Rows[0]["ten"].ToString();
            }
            catch
            {
                return "";
            }
        }

        public DataTable f_load_bn_dmbacsi()
        {
            dtoBNDMBacSi si = new dtoBNDMBacSi(this._condata);
            return si.LoadAll();
        }

        public DataTable f_load_bn_dmbenhtat()
        {
            dtoBNDMBENHTAT obndmbenhtat = new dtoBNDMBENHTAT(this._condata);
            return obndmbenhtat.LoadAll();
        }

        public DataTable f_load_bn_dmdoituong()
        {
            DataTable table = new DataTable();
            dtoBNDMDoiTuong tuong = new dtoBNDMDoiTuong(this._condata);
            return tuong.LoadAll();
        }

        public DataTable f_load_bn_dsbenhnhan()
        {
            return this.f_load_bn_dsbenhnhan("");
        }

        public DataTable f_load_bn_dsbenhnhan(string mabn)
        {
            DataTable table = new DataTable();
            string schema = this._condata.Schema;
            return this._xuli.f_Get_dulieu(string.Concat(new object[] { 
                "select a.", dtoBNThongTinBN.columnMABN, " as mabn,a.", dtoBNThongTinBN.columnHOTEN, " as hoten,a.", dtoBNThongTinBN.columnIDGIOITINH, " as idgioitinh,case when a.", dtoBNThongTinBN.columnIDGIOITINH, "=", ((HangSo.enGioiTinh) 0).GetHashCode(), " then 'Nam' else 'Nữ' end as phai,a.", dtoBNThongTinBN.columnDIACHI, " as diachi,a.", dtoBNThongTinBN.columnDIENTHOAI, " as dienthoai,a.", dtoBNThongTinBN.columnIDPHUONGXA, 
                " as idphuongxa,a.", dtoBNThongTinBN.columnNGAYSINH, " as ngaysinh,a.", dtoBNThongTinBN.columnNAMSINH, " as namsinh,b.", dtoDMPhuongXa.columnTEN, "||','||b2.", dtoDMQuanHuyen.columnTEN, "||','||b3.", dtoDMTinhThanh.columnTEN, " as tenphuongxa from ", schema, ".", dtoBNThongTinBN.tableName, " a left join ", schema, 
                ".", dtoDMPhuongXa.tableName, " b on b.", dtoDMPhuongXa.columnID, "=a.", dtoBNThongTinBN.columnIDPHUONGXA, " left join ", schema, ".", dtoDMQuanHuyen.tableName, " b2 on b2.", dtoDMQuanHuyen.columnID, "=b.", dtoDMPhuongXa.columnIDQH, " left join ", schema, 
                ".", dtoDMTinhThanh.tableName, " b3 on b3.", dtoDMTinhThanh.columnID, "=b2.", dtoDMQuanHuyen.columnIDTT, " where 1=1 ", ((mabn != "") != null) ? (" and a." + dtoBNThongTinBN.columnMABN + "='" + mabn + "'") : "", " order by a.", dtoBNThongTinBN.columnHOTEN
             })).Tables[0];
        }

        public DataTable f_loaddata_bn_dmphong(bool CoCongKham)
        {
            DataTable table = new DataTable();
            return this._xuli.f_Get_dulieu("select a." + dtoBNDMPhong.columnID + " as id,a." + dtoBNDMPhong.columnTEN + " as ten,a." + dtoBNDMPhong.columnMA + " as ma,a." + dtoBNDMPhong.columnIDVP + " as idvp,a." + dtoBNDMPhong.columnGHICHU + ((CoCongKham == null) ? ",0.0 as dongia,'' as tenvp" : (",case when b." + dtovpDMVienPhi.columnDONGIA + " is null then 0 else b." + dtovpDMVienPhi.columnDONGIA + " end as dongia,b." + dtovpDMVienPhi.columnTEN + " as tenvp")) + " from " + this._condata.Schema + "." + dtoBNDMPhong.tableName + " a" + ((CoCongKham == null) ? "" : (" left join " + this._condata.Schema + "." + dtovpDMVienPhi.tableName + " b on a." + dtoBNDMPhong.columnIDVP + "=b." + dtovpDMVienPhi.columnID))).Tables[0];
        }

        public DataTable f_loaddata_tinhphuongxa(string maviettat, long id)
        {
            string str = "";
            if (maviettat != "")
            {
                string str3 = str;
                str = str3 + " and upper(c." + dtoDMPhuongXa.columnVIETTAT + ") like '" + maviettat + "%'";
            }
            if (id != 0L)
            {
                object obj2 = str;
                str = string.Concat(new object[] { obj2, " and c.", dtoDMPhuongXa.columnID, " =", id });
            }
            string sql = "select c." + dtoDMPhuongXa.columnVIETTAT + " as viettat,c." + dtoDMPhuongXa.columnTEN + "||','||b." + dtoDMQuanHuyen.columnTEN + "||','||a." + dtoDMTinhThanh.columnTEN + " as ten,c." + dtoDMPhuongXa.columnID + ",c." + dtoDMPhuongXa.columnID + " as idphuongxa,b." + dtoDMQuanHuyen.columnID + " as idquanhuyen,a." + dtoDMTinhThanh.columnID + " as idtinhthanh from " + this._xuli.pSchema + "." + dtoDMTinhThanh.tableName + " a inner join " + this._xuli.pSchema + "." + dtoDMQuanHuyen.tableName + " b on a." + dtoDMTinhThanh.columnID + "=b." + dtoDMQuanHuyen.columnIDTT + " inner join " + this._xuli.pSchema + "." + dtoDMPhuongXa.tableName + " c on b." + dtoDMQuanHuyen.columnID + "=c." + dtoDMPhuongXa.columnIDQH + " where " + ((str == "") ? " 1=0" : (" 1=1 " + str));
            try
            {
                return this._xuli.f_Get_dulieu(sql).Tables[0];
            }
            catch
            {
                return null;
            }
        }

        public DataTable f_loaddata_vp_dmvienphi()
        {
            DataTable table = new DataTable();
            return new dtovpDMVienPhi(this._condata).LoadAll();
        }

        public DataTable f_loaddata_vp_giavp()
        {
            DataTable table = new DataTable();
            string schema = this._condata.Schema;
            return this._xuli.f_Get_dulieu("select a." + dtovpDMVienPhi.columnID + " as id,a." + dtovpDMVienPhi.columnMA + " as ma,a." + dtovpDMVienPhi.columnTEN + " as ten,a." + dtovpDMVienPhi.columnDVT + " as dvt,a." + dtovpDMVienPhi.columnDONGIA + " as dongia,a." + dtovpDMVienPhi.columnSTT + " as stt,a." + dtovpDMVienPhi.columnGHICHU + " from " + schema + "." + dtovpDMVienPhi.tableName + " a order by " + dtovpDMVienPhi.columnSTT).Tables[0];
        }
    }
}

